package com.peng.leetcode;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

/**
 * Main2
 *
 * @author: lupeng6
 * @create: 2023/3/24 15:44
 */
public class Main2 {

    public static void main(String[] args) {
        // System.out.println(new Main2().judge("()([](){})"));
        System.out.println(new Main2().judge("()([{]})"));
    }

    public boolean judge(String str) {
        LinkedList<Character> stack = new LinkedList<>();
        Map<Character, Character> pairMap = new HashMap<>();
        pairMap.put('(', ')');
        pairMap.put('[', ']');
        pairMap.put('{', '}');
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (stack.isEmpty() || pairMap.containsKey(c)) {
                stack.offerLast(c);
            } else {
                if (pairMap.get(stack.peekLast()) == c) {
                    stack.pollLast();
                } else {
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }

}
